from selenium import webdriver
import time
import random

email_addr = "pno.2719@gmail.com"
email_pass = "B166ER.1997XF11"

driver = webdriver.Firefox()
driver.get("http://www.google.com")

"""
source = unicode(driver.page_source)
source = source.encode('utf-8')
out_file = open('temp.html', 'w')
out_file.write(source)
"""

sign_in_button = driver.find_element_by_id("gbi4t")
sign_in_button.click()

time.sleep(4)
email = driver.find_element_by_id("Email")
email.send_keys(email_addr)
password = driver.find_element_by_id("Passwd")
password.send_keys(email_pass)
sign_in = driver.find_element_by_id("signIn")
sign_in.click()

time.sleep(4)
history = driver.find_element_by_id("gbgs4dn")
history.click()
history = driver.find_element_by_id("gb_150")
history.click()
time.sleep(4)
history = driver.find_elements_by_xpath("//a")
clickable = None
for i in history:
  if i.text == "Go to web history":
    clickable = i
clickable.click()

time.sleep(4)

try:
  resume_btn = driver.find_element_by_xpath(
    "//a[contains(text(), 'Resume')]")
  resume_btn.click()
except:
  pass

# This will repeat until the end has been reached.
for i in range(100):
  try:
    older_btn = driver.find_element_by_xpath(
      "//a/span[contains(text(), 'Older')]")
    older_btn.click()
    time.sleep(4)
  except:
    break
